package com.hjqjl.whdemo.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjqjl.whdemo.R;

import java.lang.ref.WeakReference;

/**
 * 旋转加载中dialog
 * DialogLoading.showLoading(this);
 * DialogLoading.stopLoading();
 */
public class DialogLoading extends Dialog {
    private WeakReference<Context> mContext;
    private volatile static DialogLoading instance;

    private TextView mTextMessage;

    private DialogLoading(Context context, CharSequence message) {
        super(context, R.style.DialogBg);

        mContext = new WeakReference<>(context);

        Window window = getWindow();
        if (window != null) {
            window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            window.setBackgroundDrawableResource(android.R.color.transparent);//去掉白色背景
            window.setContentView(R.layout.dialog_loading);
            ImageView ivLoading = window.findViewById(R.id.iv_dialog_loading);
            // 加载动画
            Animation loadingAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_roate);
            // 使用ImageView显示动画
            ivLoading.startAnimation(loadingAnimation);
            mTextMessage = (TextView) findViewById(R.id.tv_dialog_loading);
            if (!TextUtils.isEmpty(message)) {
                mTextMessage.setText(message);
            }
        }
        setCanceledOnTouchOutside(false);
    }

    private void setMessage(CharSequence message) {
        mTextMessage.setText(message);
    }

    public static synchronized void showLoading(Context context) {
        showLoading(context, "正在下载...");
    }

    public static synchronized void showLoading(Context context, CharSequence message) {
        showLoading(context, message, false);
    }

    public static synchronized void showLoading(Context context, CharSequence message, boolean cancelable) {
        if (instance != null && instance.isShowing()) {
            instance.dismiss();
        }

        if (context == null || !(context instanceof Activity)) {
            return;
        }
        instance = new DialogLoading(context, message);
        instance.setCancelable(cancelable);

        if (instance != null && !instance.isShowing() && !((Activity) context).isFinishing()) {
            instance.show();
        }
    }

    public static synchronized void changeMessage(CharSequence message) {
        if (instance != null) {
            instance.setMessage(message);
        }
    }

    public static synchronized void stopLoading() {
        if (instance != null && instance.isShowing()) {
            instance.dismiss();
        }
        instance = null;
    }

    public static boolean isLoading() {
        return instance != null && instance.isShowing();
    }
}
